﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenMetaverse;

namespace Trim
{
    public partial class ResidentSearchForm : Form
    {
        private Dictionary<string, UUID> name2key = new Dictionary<string,UUID>();

        public ResidentSearchForm()
        {
            InitializeComponent();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void QueryBox_TextChanged(object sender, EventArgs e)
        {
            SearchButton.Enabled = (QueryBox.Text.Length > 0);
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void ResultsBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ResultsBox.SelectedItem != null)
            {
                if (name2key.ContainsKey((ResultsBox.SelectedItem as string).ToLower()))
                {
                    ChooseButton.Enabled = true;
                    return;
                }
            }
            ChooseButton.Enabled = false;
        }

        private void ChooseButton_Click(object sender, EventArgs e)
        {
            if (ResultsBox.SelectedItem != null)
            {
                string lookup = (ResultsBox.SelectedItem as string).ToLower();
                if (name2key.ContainsKey(lookup))
                {
                    UUID uuid = name2key[lookup];
                    Program.client.SL.Friends.OfferFriendship(uuid);
                    Close();
                    return;
                }
            }
            ChooseButton.Enabled = false;
        }

        private void QueryBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (SearchButton.Enabled)
                {
                    e.Handled = true;
                    Search();
                }
            }
        }

        private void Search()
        {
            ChooseButton.Enabled = false;
            ResultsBox.Items.Clear();
            string query = QueryBox.Text;
            List<DirectoryManager.AgentSearchData> results = new List<DirectoryManager.AgentSearchData>();
            Program.client.SL.Directory.PeopleSearch(OpenMetaverse.DirectoryManager.DirFindFlags.People, query, 0, 10000, out results);
            if (results.Count > 0)
            {
                foreach (DirectoryManager.AgentSearchData result in results)
                {
                    string name = result.FirstName + " " + result.LastName;
                    if (!name2key.ContainsKey(name.ToLower()))
                        name2key.Add(name.ToLower(), result.AgentID);
                    ResultsBox.Items.Add(name);
                }
            }
            else
            {
                ResultsBox.Items.Add("No results for \"" + query + "\"");
            }
        }
    }
}
